** Construct shares of households in each income bracket to estimate the income elasticity of MW  
** JHL

*************************************
** Set up workspace
*************************************
version 14.0
clear all
set more off

cd "${path_home}"
adopath + ../programs

** log using "${path_log}/a03_mw_theoretical_estimates_hh", text replace

*************************************
** Start work here
*************************************

timer clear
timer on 1 


*************************************
** [1] Import CPS data 
*************************************

** Run do file on cps dat file, save as hhincome_0618

use "${path_big_dta}/cps/hhincome_0618", clear 

** keep if year==2006

** 2006/2018 
** Focus on 2006 as pre-period
** Create household income national percentiles 
foreach y of numlist 2006 {
	** _pctile hhincome [w=asecwth] if year==`y', nq(100)
	xtile hhincome_q100_y`y' = hhincome [ w = asecwth ] if year==`y', nq(100)
}

** Sort into rich and poor counties 
egen hhincome_c = wtmean(hhincome), by(county year) weight(asecwth)

drop if county == 0
egen cy_tag = tag(county year)

** Isolate and create quantiles for each unique county-year, then create for all households 
foreach y of numlist 2006 {
	** _pctile hhincome [w=asecwth] if year==`y', nq(100)
	xtile temp = hhincome_c if year==`y'&cy_tag==1, nq(2)
	egen hhincome_c_q2_y`y' = min(temp), by(county year) 
	drop temp 
}

ta hhincome_c_q2_y2006, su(hhincome) 
ta hhincome_c_q2_y2006, su(hhincome_c) 

** Create brackets for every five percentiles to match with Dube (2019) estimation  
foreach y of numlist 2006 {
	gen hhincome_q5_y`y' = 5 if hhincome_q100_y`y' <= 7.5 
		replace hhincome_q5_y`y' = 10 if hhincome_q100_y`y' <= 12.5 & hhincome_q5_y`y' == . 
		replace hhincome_q5_y`y' = 15 if hhincome_q100_y`y' <= 17.5 & hhincome_q5_y`y' == . 
		replace hhincome_q5_y`y' = 20 if hhincome_q100_y`y' <= 22.5 & hhincome_q5_y`y' == . 
		replace hhincome_q5_y`y' = 25 if hhincome_q100_y`y' <= 27.5 & hhincome_q5_y`y' == . 		
		replace hhincome_q5_y`y' = 30 if hhincome_q100_y`y' <= 32.5 & hhincome_q5_y`y' == . 		
}

** Tabulate shares of households in each national percentile bracket, by rich and poor counties
	** Shares not exact for all counties since some counties are missing 
foreach y of numlist 2006 {
	ta hhincome_q5_y`y' if year==`y' & hhincome_c_q2_y`y' == 1, m
	ta hhincome_q5_y`y' if year==`y' & hhincome_c_q2_y`y' == 2, m
	ta hhincome_q5_y`y' if year==`y', m	
}

** Tabulate shares of households in each national percentile bracket, by rich and poor counties, with sampling weights
	** Similar results 
foreach y of numlist 2006 {
	ta hhincome_q5_y`y' if year==`y' & hhincome_c_q2_y`y' == 1 [ aw = asecwth ] , m
	ta hhincome_q5_y`y' if year==`y' & hhincome_c_q2_y`y' == 2 [ aw = asecwth ] , m
	ta hhincome_q5_y`y' if year==`y' [ aw = asecwth ] , m	
}

*************************************
** Close workspace
*************************************
timer off 1
timer list 1
** log close

